package com.bms.cfcmms.service;

import com.bms.cfcmms.dao.UserDao;
import com.bms.cfcmms.model.system.User;
import com.bms.cfcmms.model.system.module.Module;
import com.bms.cfcmms.util.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by carl on 2016/4/9.
 */
@Service
public class UserService {

    final Logger log = LoggerFactory.getLogger(UserService.class);

    @Resource
    private UserDao userDao;


    public Boolean setRolesWithEmployeeId(Integer employeeId, String noderecords) {
        return false;
    }

    public User queryForObjectById(Integer employeeId){
        Module module = ModuleService.getModuleWithAsName(User.class.getSimpleName());
        return userDao.queryForObjectById(employeeId, module);
    }

    public Integer resetPassword(Integer employeeId) {

        log.info(SpringUtils.getMessage("methond.begin",
                new Object[]{"UserService", "resetPassword"}));
        Module module = ModuleService.getModuleWithAsName(User.class.getSimpleName());
        if (module == null) {
            log.error(SpringUtils.getMessage("module.notfind", User.class.getSimpleName()));
            return null;
        }

        User user = userDao.queryForObjectById(employeeId, module);
        return userDao.updateEntity(user, module);
    }

    public Boolean changePassword(String oldPassword,
                                  String newPassword, HttpServletRequest request) {
        return false;
    }

    public String genPasswordWithId(Integer userId, String password) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            log.error("", e);
        }
        if (md5 == null) {
            return null;
        }
        byte[] newstr = null;
        try {
            String encodeStr = userId.toString() + " " + password;
            newstr = Base64Utils.encode(md5.digest(encodeStr.getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {
            log.error("", e);
        }
        return new String(newstr);
    }

    public Integer saveUserIntoDb(User user) {
        Module module = ModuleService.getModuleWithAsName(User.class.getSimpleName());
        String tableName = module.getTableName();
        tableName = tableName == null ? module.getModuleName() : tableName;
        return userDao.save(user, tableName);
    }

    public List<User> queryAllUsers() {
        Module module = ModuleService.getModuleWithAsName(User.class.getSimpleName());
        return userDao.queryAllData(module);
    }

    public User queryForObjectByName(String name) {
        return userDao.queryForObjectByName(name);
    }
}
